package com.example.demo

import com.example.demo.bean.Consumer
import com.example.demo.bean.Od
import com.example.demo.bean.Room
import java.sql.ResultSet
import java.sql.Statement

object Factory {
    fun createOds(res: ResultSet, statement: Statement? = null): List<Od> = res.run {
        val list = mutableListOf<Od>()
        while (next()) {
            list.add(
                Od(
                    getString("orderId"),
                    getString("roomId"),
                    getString("consumerId"),
                    getString("startTime"),
                    getString("endTime"),
                    getString("extra"),
                    getInt("state"),
                    getInt("allCost")
                )
            )
        }
        res.close()
        statement?.close()
        list
    }

    fun createRooms(res: ResultSet, statement: Statement? = null): List<Room> = res.run {
        val list = mutableListOf<Room>()
        while (next()) {
            list.add(
                Room(
                    roomId = getString("roomId"),
                    cost = getInt("cost"),
                    type = getString("type"),
                    state = getInt("state")
                )
            )
        }
        res.close()
        statement?.close()
        list
    }

    fun createConsumers(res: ResultSet, statement: Statement? = null): List<Consumer> =
        mutableListOf<Consumer>().apply {
            res.apply {
                while (next()) {
                    add(Consumer(getString("consumerId"), getString("name"), getString("phoneNumber")))
                }
                close()
            }
            statement?.close()
        }


}

